//
//  SuXiBaseNavVC.swift
//  KM_MoonTools
//
//  Created by Li Dong on 2023/4/26.
//

import UIKit

class SuXiBaseNavVC: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationBgColor = UIColor.init(hex: 0xD4DFFF)
        navigationBar.isTranslucent = false
        UINavigationBar.appearance().tintColor = UIColor.init(hex: 0x3388FA)
    }
    

    var navigationBgColor: UIColor? {
        didSet {
            if #available(iOS 15.0, *) {
                let navBarAppearance = UINavigationBarAppearance()
                navBarAppearance.backgroundColor = navigationBgColor
//                if CGFloat.topPadding > 20 {
//                    navBarAppearance.shadowImage = UIImage.jw_getImageWithColor(color: UIColor.clear)
//                }
                navBarAppearance.titleTextAttributes = [NSAttributedString.Key.font : UIFont.Medium(ofSize: 16) as Any,NSAttributedString.Key.foregroundColor : UIColor.black]
                navigationBar.scrollEdgeAppearance = navBarAppearance
                navigationBar.standardAppearance = navBarAppearance
            }else{
                self.navigationController?.view.backgroundColor = UIColor.white
                navigationBar.barTintColor = navigationBgColor
//                if CGFloat.topPadding > 20 {
//                    navigationBar.shadowImage = UIImage()
//                }
                navigationBar.titleTextAttributes = [NSAttributedString.Key.font : UIFont.Medium(ofSize: 16) as Any,NSAttributedString.Key.foregroundColor : UIColor.black]
            }
        }
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            viewController.navigationItem.leftBarButtonItem =  UIBarButtonItem(image: UIImage(named: "arrow_back")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), style: .done, target: self, action: #selector(backAction))
            interactivePopGestureRecognizer?.delegate = viewController as? UIGestureRecognizerDelegate
        }
        super.pushViewController(viewController, animated: animated)
        self.interactivePopGestureRecognizer?.isEnabled = true
    }
    

    @objc func backAction(){
        popViewController(animated: true)
    }
    
    func findHairlineImageViewUnder(tempView: UIView) -> UIImageView? {
        if let temp = tempView as? UIImageView, tempView.size.height <= 1 {
            return temp
        }
        for sub in tempView.subviews {
            let tt = findHairlineImageViewUnder(tempView: sub)
            if  tt != nil {
                return tt
            }
        }
        return nil
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return topViewController?.preferredStatusBarStyle ?? .default
    }

}
